<?require("admin-header.php");
if (!(isset($_SESSION['administrator']))){
	echo "<a href='../loginpage.php'>Please Login First!</a>";
	exit(1);
}?>
<?
if(isset($_POST['do'])){
	if (isset($_POST['rjpid'])){
		$rjpid=intval($_POST['rjpid']);
		$sql="select * from `solution` WHERE `problem_id`=".$rjpid;
		$result=mysql_query($sql) or die(mysql_error());
		while($row=mysql_fetch_object($result))
		{
		$address = $managerAddress;
		$port = $managerPort;

		$buff=$row->solution_id.'$$$$$'.$row->problem_id.'$$$$$'.$row->language.'$$$$$'.'rejudge'.'$$$$$'.$row->user_id;
		echo $buff;
		if(!socket_sendto($sock,$buff,65555,0,$address,$port))
		echo "socket_write() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
		
		$sql="delete from `sim` WHERE `s_id` in (select solution_id from solution where `problem_id`=".$rjpid.")";
		mysql_query($sql) or die(mysql_error());
		$url="../status.php?problem_id=".$rjpid;
	 	echo "Rejudged Problem ".$rjpid;
		echo "<script>location.href='$url';</script>";
		}
 	}
	if (isset($_POST['rjsid'])){
		$rjsid=intval($_POST['rjsid']);
		$sql="select * from `solution` WHERE `solution_id`=".$rjsid;
		$result=mysql_query($sql) or die(mysql_error());
		$rows=mysql_num_rows($result);
		echo $rows;
		while($row=mysql_fetch_object($result))
		{
		$address = $managerAddress;
		$port = $managerPort;
		
	 	$buff=$row->solution_id.'$$$$$'.$row->problem_id.'$$$$$'.$row->language.'$$$$$'.'rejudge'.'$$$$$'.$row->user_id;
		echo $buff;
		if(!socket_sendto($sock,$buff,65555,0,$address,$port))
		echo "socket_write() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
		
		
		$sql="delete from `sim` WHERE `s_id`=".$rjsid;
		mysql_query($sql) or die(mysql_error());
		$url="../status.php?top=".($rjsid+1);
	 	echo "Rejudged Runid ".$rjsid;
		echo "<script>location.href='$url';</script>";
		}
	
	}
}
?>
<b>Rejudge</b>
	<ol>
	<li>Problem
	<form action='rejudge.php' method=post>
		<input type=input name='rjpid'>	<input type='hidden' name='do' value='do'>
		<input type=submit value=submit>
	</form>
	<li>Solution
	<form action='rejudge.php' method=post>
		<input type=input name='rjsid'>	<input type='hidden' name='do' value='do'>
		<input type=submit value=submit>
	</form>
